﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CongregacaoOs.Data;
using CongregacaoOs.Business;

namespace CongregacaoOs.WebForm.Cadastros
{
    public partial class Setor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (Session["DadosGerais"] == null) Response.Redirect("~/Cadastros/Account/Login.aspx");
            if (Session["TipoUsuario"].ToString() == "Local") Response.Redirect("~/Cadastros/Home.aspx");

            Page.Form.DefaultButton = btnBuscar.UniqueID;

            if (!IsPostBack)
            {
                AtualizaGridView();
                AtualizarCargos();
            }
        }

        public void AtualizaGridView()
        {
            ccSetorBus bus = new ccSetorBus();
            ccSetor ent = new ccSetor();

            ent.nome = txtBuscarNome.Text;
            ent.idSetor = 0;

            gvSetor.DataSource = bus.SetorSelect(ent);
            gvSetor.DataBind();
        }

        public void AtualizarCargos()
        {
            ccCargoBus bus = new ccCargoBus();
            ccCargo ent = new ccCargo();

            listCargos.DataSource = bus.CargoSelect(ent);
            listCargos.DataBind();
        }

        #region Moving Itens in ListBox

        private void MoverItems(bool isAdd)
        {
            if (isAdd) //means if you add items to the right box
            {
                for (int i = listCargos.Items.Count - 1; i >= 0; i--)
                {
                    if (listCargos.Items[i].Selected)
                    {
                        ListCargosResult.Items.Add(listCargos.Items[i]);
                        ListCargosResult.ClearSelection();
                        listCargos.Items.Remove(listCargos.Items[i]);
                    }
                }
            }
            else // means if you remove items from the right box and add it back to the left box
            {
                for (int i = ListCargosResult.Items.Count - 1; i >= 0; i--)
                {
                    if (ListCargosResult.Items[i].Selected)
                    {
                        listCargos.Items.Add(ListCargosResult.Items[i]);
                        listCargos.ClearSelection();
                        ListCargosResult.Items.Remove(ListCargosResult.Items[i]);
                    }
                }
            }
        }

        private void MoveAllItems(bool isAddAll)
        {
            if (isAddAll) //means if you add ALL items to the right box
            {
                for (int i = listCargos.Items.Count - 1; i >= 0; i--)
                {
                    ListCargosResult.Items.Add(listCargos.Items[i]);
                    ListCargosResult.ClearSelection();
                    listCargos.Items.Remove(listCargos.Items[i]);
                }
            }
            else // means fi you remove all items from the right box and add it back to the left box 
            {
                for (int i = ListCargosResult.Items.Count - 1; i >= 0; i--)
                {
                    listCargos.Items.Add(ListCargosResult.Items[i]);
                    listCargos.ClearSelection();
                    ListCargosResult.Items.Remove(ListCargosResult.Items[i]);
                }
            }
        }

        #endregion    Moving Itens in ListBox



        protected void gvSetor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            AtualizaGridView();
            gvSetor.PageIndex = e.NewPageIndex;
            gvSetor.DataBind();
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {

                AtualizaGridView();
        }

        protected void gvSetor_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgEditar = (e.Row.FindControl("imgEditar") as ImageButton);
                imgEditar.CommandArgument = e.Row.RowIndex.ToString();

                ImageButton imgDelete = (e.Row.FindControl("imgDelete") as ImageButton);
                imgDelete.CommandArgument = e.Row.RowIndex.ToString();

                imgDelete.Attributes.Add("onclick", "javascript:return " +
                        "confirm('Confirmar exclusão do registro: " +
                        DataBinder.Eval(e.Row.DataItem, "nome") + "?')");
            }
        }

        protected void gvSetor_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EDITAR" || e.CommandName == "EXCLUIR")
            {
                LinkButton linkID = new LinkButton();
                int index = int.Parse(e.CommandArgument.ToString());

                HiddenField hdIdSetor = (gvSetor.Rows[index].FindControl("hdIdSetor") as HiddenField);
                hdID.Value = hdIdSetor.Value;

                if (e.CommandName == "EDITAR")
                {
                    hfIndexAccordion.Value = "1";
                    txtNome.Text = gvSetor.Rows[index].Cells[2].Text.Trim();


                    ccSetorCargosBus bus = new ccSetorCargosBus();

                    ListCargosResult.Items.Clear();
                    List<ccSetorCargosSelect_Result> listaCargo = bus.SetorCargosSelect(Convert.ToInt32(hdID.Value));
                    if(listaCargo != null)
                    for (int i = listaCargo.Count - 1 ; i >= 0; i--) 
                    {

                        ListCargosResult.Items.Add(new ListItem(listaCargo[i].Cargo, listaCargo[i].idCargo.ToString()));
                        ListCargosResult.ClearSelection();
                        listCargos.Items.Remove(new ListItem(listaCargo[i].Cargo,listaCargo[i].idCargo.ToString()));

                    }

                }
                else if (e.CommandName == "EXCLUIR")
                {
                    ccSetorBus bus = new ccSetorBus();
                    ccSetorCargosBus scBus = new ccSetorCargosBus();
                    scBus.SetorCargosDeleteBySetor(Convert.ToInt32(hdID.Value));
                    bus.SetorDelete(Convert.ToInt32( hdID.Value));
                    AtualizaGridView();
                }
            }
        }

        protected void btnAdicionarNovo_Click(object sender, EventArgs e)
        {
            hdID.Value = null;
            hfIndexAccordion.Value = "1";
            txtNome.Text = string.Empty;
            ListCargosResult.Items.Clear();
            AtualizarCargos();
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {

            try
            {
                int idSetorC = 0;

                ccSetorBus bus = new ccSetorBus();
                ccSetor ent = new ccSetor();

                ccSetorCargosBus scBus = new ccSetorCargosBus();
                ccSetorCargos scEnt = new ccSetorCargos();

                ent.nome = txtNome.Text.Trim();

                if (hdID.Value != "")
                {
                    ent.idSetor = Convert.ToInt32(hdID.Value);
                    scBus.SetorCargosDeleteBySetor(ent.idSetor);
                }


                idSetorC = bus.SetorSaveOrUpdate(ent);





                for (int i = ListCargosResult.Items.Count - 1; i >= 0; i--)
                {
                    scEnt.idCargo = Convert.ToInt32(ListCargosResult.Items[i].Value);
                    scEnt.idSetor = idSetorC != 0 ? idSetorC : Convert.ToInt32(hdID.Value);
                    scBus.SetorCargosSaveOrUpdate(scEnt);
                }

                hfIndexAccordion.Value = "0";
                AtualizaGridView();
                AtualizarCargos();
                ListCargosResult.Items.Clear();
                Functions.LimparCampos(this);


                MessageWindow.MessageType = true;
                MessageWindow.Texto = "Registro salvo com sucesso!!!";
                MessageWindow.ShowWindow();

            }
            catch (Exception)
            {

                MessageWindow.MessageType = false;
                MessageWindow.Texto = "Houve um erro ao salvar o registro. Tente novamente ou contate o suporte";
                MessageWindow.ShowWindow();
               
            }
           
        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            hfIndexAccordion.Value = "0";
            AtualizarCargos();
            ListCargosResult.Items.Clear();
            Functions.LimparCampos(this);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            MoverItems(true);
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            MoverItems(false);
        }

        protected void btnAddAll_Click(object sender, EventArgs e)
        {
            MoveAllItems(true);
        }

        protected void btnRemoveAll_Click(object sender, EventArgs e)
        {
            MoveAllItems(false);
        }
    }
}